package ar.edu.itba.it.cg.grupo01.game.sound;

import java.net.URL;

import ar.edu.itba.it.cg.grupo01.utils.Utils;

import com.jmex.audio.AudioSystem;
import com.jmex.audio.AudioTrack;
import com.jmex.audio.AudioTrack.TrackType;


public class Pioneer {

	private final String backgroundMusicPath = "music/music/music1.wav";
	private final String backgroundEnginePath = "music/engine/EngineRev2.ogg";
	
	private AudioTrack backgroundMusic;
	private AudioTrack backgroundEngine;
	private AudioSystem audioSystem = AudioSystem.getSystem();
	
	private boolean musicIsOn = true;
	private boolean sfxIsOn = true;

	private float musicVolume;
	private float sfxVolume;
	
	public Pioneer(boolean musicIsOn, boolean sfxIsOn){
		this.musicIsOn = musicIsOn;
		this.sfxIsOn = sfxIsOn;
		this.musicVolume = 1.0f;
		this.sfxVolume = 1.0f;
		
		loadBackGroundMusic();
		updateMusicState();
		
		loadBackGroundEngineSound();
		updateEngineState();
	}

	private void loadBackGroundEngineSound() {
		try {
			backgroundEngine = getMusic(Utils.getResourceURL(backgroundEnginePath));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
//		System.out.println("Music loaded");
		
	}

	public void pauseAllSounds(){
		backgroundMusic.pause();
		backgroundEngine.pause();
	}
	
	public void resumeAllSounds(){
		backgroundMusic.play();
		backgroundEngine.play();
	}
	
	private void loadBackGroundMusic(){
		try {
			backgroundMusic = getMusic(Utils.getResourceURL(backgroundMusicPath));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
//		System.out.println("Music loaded");
	}

		
	private boolean musicIsOn(){
		return musicIsOn;
	}
	
	private boolean sfxIsOn(){
		return sfxIsOn;
	}
	
	public void destroy() {
		audioSystem.cleanup();
	}

	private void updateEngineState(){
		backgroundEngine.setVolume(sfxVolume);
		
		if(sfxIsOn()){
			if(!backgroundEngine.isPlaying())
				backgroundEngine.play();
		}
		else{
			backgroundEngine.stop();
		}
	}
	
	private void updateMusicState(){
		backgroundMusic.setVolume(musicVolume);
		
		if(musicIsOn()){
			if(!backgroundMusic.isPlaying())
				backgroundMusic.play();
		}
		else{
			backgroundMusic.stop();
		}
	}
	
	public void enableMusic(boolean onOff){
		musicIsOn = onOff;
		updateMusicState();
	}
	
	public void enableSFX(boolean onOff){
		sfxIsOn = onOff;
		updateEngineState();
	}
	
	public void setSFXVolume(float volume){
		sfxVolume = volume;
        System.out.println("fx volume = " + volume);
		backgroundEngine.setVolume(volume);
	}
	
	public void setMusicVolume(float volume){
		musicVolume = volume;
        System.out.println("music volume = " + volume);
		backgroundMusic.setVolume(volume);
	}
	
	public void playEffect(PioneerSoundSystem audio, boolean shouldLoop) {
		AudioTrack sfxTrack;
		
		if(sfxIsOn()){
			sfxTrack = audio.getTrack(shouldLoop);
			sfxTrack.setVolume(sfxVolume);
			sfxTrack.play();
		}
	}

	
	private AudioTrack getMusic(URL pathToMusic) {
	      // Create streaming, looping, non-relative music clip.
	      AudioTrack sound = audioSystem.createAudioTrack(pathToMusic, false);
	      sound.setType(TrackType.MUSIC);
	      sound.setRelative(false);
	      sound.setVolume(musicVolume);
	      sound.setLooping(true);
	      return sound;
	   }
}